package cn.aixuxi.security.uaa.service.impl;

import cn.aixuxi.security.uaa.entity.SecurityUser;
import cn.aixuxi.security.uaa.entity.SysUser;
import cn.aixuxi.security.uaa.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 类描述
 *
 * @author ruozhuliufeng
 * @version 1.0
 * @date 2021/4/14 21:35
 */
@Service("userDetailsService")
public class UserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    private SysUserService sysUserService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        // 根据用户名查询数据
        SysUser sysUser = sysUserService.selectByUsername(username);
        // 判断用户是否为空
        if (sysUser == null){
            throw new UsernameNotFoundException("用户不存在");
        }
        SecurityUser securityUser = new SecurityUser();
        securityUser.setCurrentUserInfo(sysUser);
        List<String> permissionValueList = new ArrayList<>();
        permissionValueList.add("test:view");
        securityUser.setPermissionValueList(permissionValueList);
        return securityUser;
    }
}
